from uuid import uuid1
from conn_mysql import MysqlApi
import datetime

class Token():

        
    def insert_token(self, company_id):
        '''插入token值返回token值
        '''

        token = uuid1().hex

        with MysqlApi() as MA:

            MA.insert_token(token, company_id)

        return token

       
    def token_status(self, token):#token值是否有效

        if token == None:

            return {

                "status": -1,
                "errorCode": 101,
                "errorMsg": "无效的token值",
                "resultBody": None          
            }

        with MysqlApi() as MA:#取出token值

            token_info = MA.select_token(token)

        if token_info:
            token_time = token_info['regedate']
            company_id = token_info['company_id']
            print(company_id)
            with MysqlApi() as MA:
                company_info = MA.select_for_company_id(token_info['company_id'])
            if token_time + datetime.timedelta(hours = 9) > datetime.datetime.now():#检验token有效性
                return {

                    "status": 1,
                    "errorCode": None,
                    "errorMsg": None,
                    "resultBody": {'token_info': token_info, "company_info": company_info}          
                }

        return {

                "status": -1,
                "errorCode": 101,
                "errorMsg": "无效的token值",
                "resultBody": None          
            }

# x = Token()
# x.make_token()
    
        


